package com.cws.project.cws.graduation.audit.controller;

import com.cws.common.utils.StringUtils;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.framework.web.page.TableDataInfo;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.service.IClazzUserService;
import com.cws.project.cws.graduation.audit.domain.GraduationAudit;
import com.cws.project.cws.graduation.audit.service.IGraduationAuditService;
import com.cws.project.cws.student.domain.Student;
import com.cws.project.cws.student.service.IStudentService;
import com.cws.project.system.dict.utils.DictUtils;
import com.cws.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 毕业审核
 * @author XYL
 * @date 2022-01-23 14:10
 */
@Controller
@RequestMapping("/cws/graduation/audit")
public class GraduationAuditController extends BaseController {
    private String prefix = "cws/graduation/audit";

    @Autowired
    private IGraduationAuditService graduationAuditService;
    @Autowired
    private IClazzUserService clazzUserService;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private IStudentService studentService;

    @RequiresPermissions("cws:graduation:audit:view")
    @GetMapping()
    public String project() {
        return prefix + "/audit";
    }

    /**
     * 查询毕设内容列表
     */
    @RequiresPermissions("cws:graduation:audit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GraduationAudit graduationAudit) {
        startPage();
        User sysUser = getSysUser();
        if(sysUser.isTeacher()){
            graduationAudit.setTeacherId(sysUser.getUserId());
        }else if(sysUser.isStudent()){
            graduationAudit.setStudentId(sysUser.getUserId());
        }else if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            graduationAudit.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        List<GraduationAudit> list = graduationAuditService.selectGraduationAuditList(graduationAudit);
        return getDataTable(list);
    }


    /**
     * 修改毕设内容
     */
    @GetMapping("/edit/{graduationProductId}")
    public String edit(@PathVariable("graduationProductId") Long graduationProductId, ModelMap mmap) {
        GraduationAudit graduationProject = graduationAuditService.selectGraduationAuditById(graduationProductId);
        mmap.put("graduationProject", graduationProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存毕设内容
     */
    @RequiresPermissions("cws:graduation:audit:edit")
    @Log(title = "毕业审核内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GraduationAudit graduationAudit) {
        return toAjax(graduationAuditService.updateGraduationAudit(graduationAudit));
    }
    /**
     * 发送邮件
     */
    @RequiresPermissions("cws:graduation:send:email")
    @Log(title = "邮件发送", businessType = BusinessType.SEND)
    @PostMapping("/send/email")
    @ResponseBody
    public AjaxResult sendEmail(GraduationAudit audit) {
        GraduationAudit graduationAudit = graduationAuditService.selectGraduationAuditById(audit.getGraduationProductId());
        Student student = studentService.selectUserById(graduationAudit.getStudentId());
        if(StringUtils.isBlank(student.getEmail())){
            return AjaxResult.error("发送失败，此学生邮箱为空！");
        }

        GraduationAudit ga = new GraduationAudit();
        ga.setStudentId(student.getUserId());
        List<GraduationAudit> graduationAudits = graduationAuditService.selectGraduationAuditList(ga);
        if(graduationAudits.size() == 0){
            return AjaxResult.error("查询失败！");
        }
        GraduationAudit gaa = graduationAudits.get(0);
        String str = "平均gpa:" + gaa.getAvgGpa() + "；挂科数：" + gaa.getFailScoreCount() + "；毕业审核状态：" + graduationAudit.getAuditStatus();
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom("1973812565@qq.com");
        message.setTo(student.getEmail());
        message.setSubject("毕设审核提醒");
        message.setText(str);
        javaMailSender.send(message);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return toAjax(true);
    }

}
